#!/bin/bash
# Copyright (c) 2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
set +e
echo -e "\n\033[32m\t*********Welcome to sharekit!*********\033[0m\n"
echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

ROOT_DIR=$(pwd)
TARGET=sharekit_driver

function check_shell_environment() {
  case $(uname -s) in 
    Linux)
          shell_result=$(/bin/sh -c 'echo ${BASH_VERSION}')
          if [ -n "${shell_result}" ]; then
            echo -e "\033[32mSystem shell: bash ${shell_result}\033[0m"
          else
            echo -e "\033[33m Your system shell isn't bash, we recommend you to use bash, because some commands may not be supported in other shells, such as pushd and shopt are not supported in dash. \n You can follow these tips to modify the system shell to bash on Ubuntu: \033[0m"
            echo -e "\033[33m [1]:Open the Terminal tool and execute the following command: sudo dpkg-reconfigure dash \n [2]:Enter the password and select <no>  \033[0m"
          fi
          ;;
    Darwin)
          echo -e "\033[31m[OHOS ERROR] Darwin system is not supported yet\033[0m"
          ;;
    *)
          echo -e "\033[31m[OHOS ERROR] Unsupported this system: $(uname -s)\033[0m"
          exit 1
  esac
}

check_shell_environment

echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo -e "\033[32mCurrent time: $(date +%F' '%H:%M:%S)\033[0m"
echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo -e "\033[32mBuild args: $@\033[0m"
echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

# 显示用法函数
usage() {
    cat << EOF
    Sharekit third party 交叉编译脚本

    用法: $0 [选项]

    选项:
    --target=TARGET               编译目标
                                  支持: sharekit_driver(默认),demo,third_party
    --compile-prefix=TARGET       编译目标
                                  支持: sharekit_driver(默认),demo,third_party
    --clean                       清理编译文件
    --help                        显示此帮助信息


    示例:
    # 交叉编译 AArch64 三方库
    $0 --target=sharekit_driver
    $0 --target=third_party
    
    # 删除三方库
    $0 --clean
EOF
}

clean_build() {
    echo "=== 清理编译文件 ==="
    rm -rf ${ROOT_OUTPUT}
    rm -rf ${ROOT_INSTALL}
    echo "=== 清理完成 ==="
}

check_command_line() {
    while [ $# -gt 0 ]; do
        case $1 in
            --target=*)
                TARGET="${1#*=}"
                ;;
            --clean)
                clean_build
                exit 0
                ;;
            --help)
                usage
                exit 0
                ;;
            *)
                echo "错误: 未知选项 $1"
                usage
                exit 1
                ;;
        esac
        shift
    done
}

check_command_line "$@"

main() {
    if [[ "$TARGET" -eq "sharekit_driver" ]]; then
      make ${TARGET}
    elif [[ "$TARGET" -eq "demo" ]]; then
      make ${TARGET}
    else
      souorce ./build_thirdparty.sh
    fi
}

main "$@"
